package logic

import (
	"context"
	"fmt"
	"week2/pkg"
	"week2/user_srv/user"

	"week2/api/internal/svc"
	"week2/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsCodeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSendSmsCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsCodeLogic {
	return &SendSmsCodeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SendSmsCodeLogic) SendSmsCode(req *types.SendSmsCodeRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 发送短信验证码

	//生成短信验证码
	num := pkg.NumRandSmsCode()
	fmt.Println("生成短信验证码是.............", num)

	pkg.SmsCode(req.Mobile, num) //调用第三方接口发送短信

	_, err = l.svcCtx.UserSrv.SendSmsCode(l.ctx, &user.SendSmsCodeRequest{
		Mobile: req.Mobile,
		Source: req.Source,
		Code:   num,
	})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:  -1,
			Msg:  "短信验证码发送失败",
			Data: nil,
		}, nil
	}

	return &types.RegMsgResponse{
		Cre:  0,
		Msg:  "短信验证码发送成功",
		Data: nil,
	}, nil

}
